package com.tanhua.manage.listener;

import com.tanhua.commons.templates.HuaWeiUGCTemplate;
import com.tanhua.domain.mongo.Publish;
import com.tanhua.dubbo.api.PublishApi;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@RocketMQMessageListener(topic = "tanhua_publish",consumerGroup = "tanhua-publish-consumer")
@Slf4j
public class PublishMessageListener implements RocketMQListener<String> {

    @Reference
    private PublishApi publishApi;

    @Autowired
    private HuaWeiUGCTemplate huaWeiUGCTemplate;

    @Override
    public void onMessage(String publishId) {
        log.info("消息监听器：动态内容审核，动态ID：" + publishId);
        Publish publish = publishApi.findById(publishId);
        Integer state = publish.getState();
        if(publish != null && state == 0){
            boolean textEable = huaWeiUGCTemplate.textContentCheck(publish.getTextContent());
            if(textEable){
                boolean imageEanble = huaWeiUGCTemplate.imageContentCheck(publish.getMedias().toArray(new String[]{}));
                if (imageEanble){
                    state = 1;
                }else{
                    state = 2;
                }
            }else{
                state = 2;
            }
        }
        publishApi.updateById(publishId,state);
    }
}
